package com.zzty.txj.controller;


import com.zzty.txj.entity.*;
import com.zzty.txj.service.ReportService;
import com.zzty.txj.util.ExcelUtils;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@RequestMapping(value = "/report")
@RestController
public class ReportController {
    private static final org.slf4j.Logger logger = LoggerFactory.getLogger(ReportController.class);
@Autowired
    ReportService reportService;
    @RequestMapping("/getUser")
   public List<Count>counts(Integer userId, String date1, String date2, HttpServletResponse response)throws Exception{

        List<DoDaily>doDailies=reportService.dodaily(userId,date1,date2);
        List<Count>counts=new ArrayList<>();
        Integer count1=0;
        String a=reportService.dailyId(userId).replaceAll(",","");

        for(int i=0;i<a.length();i++){
            Count count=new Count();
            count.setStartDate(date1);
            count.setFinishDate(date2);
            count.setName(reportService.getName(doDailies.get(i).getUserId()));
            count.setSubject(reportService.getSubjectName(doDailies.get(i).getDailyId()));
            count.setFinishTimes(reportService.counts(userId,date1,date2,doDailies.get(i).getDailyId()));

            counts.add(count);
        }

        logger.info("ReportController/getUser"+counts);
        return counts;


    }


    @RequestMapping("/excel")
    public void excel(Integer userId, String date1, String date2, HttpServletResponse response)throws Exception{
        List<DoDaily>doDailies=reportService.dodaily(userId,date1,date2);
        List<Count>counts=new ArrayList<>();
        Integer count1=0;
//        String a=reportService.dailyId(userId).replaceAll(",","");
        String a=reportService.dailyId(1).replaceAll(",","");

        System.out.println("a"+a.length());
        ExcelData data = new ExcelData();
        for(int i=0;i<a.length();i++){
            Count count=new Count();
            count.setStartDate(date1);
            count.setFinishDate(date2);
//            count.setName(reportService.getName(doDailies.get(i).getUserId()));
//            count.setSubject(reportService.getSubjectName(doDailies.get(i).getDailyId()));
//            count.setFinishTimes(reportService.counts(userId,date1,date2,doDailies.get(i).getDailyId()));
            count.setName("0");
            count.setSubject("0");
            count.setFinishTimes(0);
            System.out.println("report"+count);
            counts.add(count);

            data.setName("用户信息数据");

            //添加表头
            List<String> titles = new ArrayList();
            List<List<Object>> rows = new ArrayList();
            List<Object> row = null;
//        for(int i=1; i<excelInfo.getNames().length;i++){
            row=new ArrayList();
            for(int j=0;j<a.length();j++){
        titles.add(count.getStartDate());
        titles.add(count.getFinishDate());
        titles.add(count.getName());
        titles.add(count.getSubject());
        titles.add(String.valueOf(count.getFinishTimes()));
                data.setTitles(titles);
                row.add(count.getStartDate());
                row.add(count.getFinishDate());
                row.add(count.getName());
                row.add(count.getSubject());
                row.add(String.valueOf(count.getFinishTimes()));
                rows.add(row);
            }
            data.setRows(rows);
            //添加列
        }


        SimpleDateFormat fdate=new SimpleDateFormat("yyyy-MM-dd-HH-mm-ss");
        String fileName=fdate.format(new Date())+".xls";
        ExcelUtils.exportExcel(response, fileName, data);
        System.out.println("report"+counts.toString());

    }
}
